<!--title-->
<div class="mnav1">
	<div class="m_nav">
		<div class="nav_left">
			<em class="home_icon"><?=$this->sys_name?></em><span>&nbsp;&gt;&nbsp;</span><font><?=$this->title?></font>
		</div>
	</div>
</div>

<div class="content-table">
<div class="content-table-title"><?=$this->title?></div>
<br />
<?php if($this->msg):?>
<div class="replace"><?=$this->msg?></div>
<br />
<?php endif;?>
<div class="form-edit-task">

<form action="/contracts/edit-contract-type2-submit" method="post" name="form1" onsubmit="submitform(this);return false;">
<table border="1" width="100%">
	<tr>
		<td align="right" width="30%">客户全称：</td>
		<td align="left">&nbsp;<?=$_SESSION['contract-edit-type2'][$this->contract_id]['client_info']['client_name']?></td>
	</tr>
	<?php if($_SESSION['contract-edit-type2'][$this->contract_id]['client_info']['additional_info']['brand_info']['str']):?>
	<tr>
		<td align="right" width="30%">品牌：</td>
		<td align="left">&nbsp;<?=$_SESSION['contract-edit-type2'][$this->contract_id]['client_info']['additional_info']['brand_info']['str']?></td>
	</tr>
	<?php endif;?>
	<?php if($_SESSION['contract-edit-type2'][$this->contract_id]['client_info']['additional_info']['factory_info']['str']):?>
	<tr>
		<td align="right" width="30%">厂商：</td>
		<td align="left">&nbsp;<?=$_SESSION['contract-edit-type2'][$this->contract_id]['client_info']['additional_info']['factory_info']['str']?></td>
	</tr>
	<?php endif;?>
	<?php if($_SESSION['contract-edit-type2'][$this->contract_id]['client_info']['additional_info']['channel_info']['str']):?>
	<tr>
		<td align="right" width="30%">渠道：</td>
		<td align="left">&nbsp;<?=$_SESSION['contract-edit-type2'][$this->contract_id]['client_info']['additional_info']['channel_info']['str']?></td>
	</tr>
	<?php endif;?>
	<tr>
		<td align="right">签约公司：</td>
		<td align="left"><select name="signer_id" id="signer_id">
				<option value="">【请选择】</option>
				<option value="same" <?php if(!$_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['signer_id'] || 'same'==$_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['signer_id']){echo "selected='selected'";}?>>【同客户】</option>
				<?php if(!empty($this->signers)):?>
				<?php foreach($this->signers as $signer_id => $signer_name):?>
				<option value="<?=$signer_id?>" <?php if($signer_id==$_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['signer_id']){echo "selected='selected'";}?>><?=$signer_name?></option>
				<?php endforeach;?>
				<?php endif;?>
			</select><font color="red"> *</font>
		</td>
	</tr>
	<tr>
		<td align="right">乙方单位：</td>
		<td align="left"><select name="owner_id" id="owner_id">
				<option value="">【请选择】</option>
				<?php if(!empty($this->owners)):?>
				<?php foreach($this->owners as $owner_id => $owner_name):?>
				<option value="<?=$owner_id?>" <?php if($owner_id==$_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['owner_id']){echo "selected='selected'";}?>><?=$owner_name?></option>
				<?php endforeach;?>
				<?php endif;?>
			</select><font color="red"> *</font>
		</td>
	</tr>
	<tr>
		<td align="right" width="30%">销售顾问：</td>
		<td align="left">&nbsp;<?=$_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['sales_emp_name']?></td>
	</tr>
	<tr>
		<td align="right">起止日期：</td>
		<td align="left">&nbsp;
			<?php if($_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['start_date'] || $_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['expired_date']):?>
				<?=$_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['start_date']?> 至 <?=$_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['expired_date']?>
			<?php else:?>
				在产品中选择日期
			<?php endif;?>
		</td>
	</tr>
	<tr>
		<td align="right">合同标准金额：</td>
		<td align="left">&nbsp;<?=$_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['contract_claim_amount']?></td>
	</tr>
	<tr>
		<td align="right">合同实际金额：</td>
		<td align="left">&nbsp;<?=$_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['contract_value']?></td>
	</tr>
	<tr>
		<td align="right">应收金额：</td>
		<td align="left">&nbsp;<em id="receivable_value"><?=($_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['contract_value']-$_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['contract_cost'])?($_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['contract_value']-$_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['contract_cost']):0?></em></td>
	</tr>
	<tr>
		<td align="right">折扣：</td>
		<?php 
		if($_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['contract_claim_amount'])
		{
			$discount = ($_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['contract_value'])/($_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['contract_claim_amount']*2)*100;
		}else{
			$discount = "";
		}
		?>
		<td align="left">&nbsp;<em id="receivable_value"><?=is_numeric($discount)?round($discount)."%":""?></em></td>
	</tr>
	<tr>
		<td align="right">付款方式：</td>
		<td align="left">&nbsp;<select name="pay_type" id="pay_type">
				<option value="1" <?php if(1==$_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['pay_type']){echo "selected='selected'";}?>>预付</option>
				<option value="2" <?php if(2==$_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['pay_type']){echo "selected='selected'";}?>>后付</option>
				<option value="3" <?php if(3==$_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['pay_type']){echo "selected='selected'";}?>>分期</option>
				<option value="4" <?php if(4==$_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['pay_type']){echo "selected='selected'";}?>>免费</option>
			</select>
		</td>
	</tr>
	<tr>
		<td align="right">签约类型：</td>
		<td align="left">&nbsp;<select name="sign_type" id="sign_type" onchange="DisplaySales(this.options[this.options.selectedIndex].value);">
				<option value="1" <?php if(2!=$_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['sign_type']){echo "selected='selected'";}?>>单签</option>
				<option value="2" <?php if(2==$_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['sign_type']){echo "selected='selected'";}?>>集采</option>
			</select><em id="key_account_sales_input">
			&nbsp;&nbsp;&nbsp;&nbsp;
			<select name="key_account_sales" id="key_account_sales">
				<option value="">选择大客户销售</option>
				<?php foreach($this->sales as $emp_id => $name):?>
					<option value="<?=$emp_id?>" <?php if($emp_id==$_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['key_account_sales']){echo "selected='selected'";}?>><?=$name?></option>
				<?php endforeach;?>
			</select>
			<font color="red"> *</font></em>
		</td>
	</tr>
	<tr>
		<td align="right" width="30%">申请票种：</td>
		<td align="left">&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="invoice_type" id="invoice_type_1" value="1" <?php if(1==$_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['invoice_type']){echo "checked='checked'";}?> />&nbsp;<label for="invoice_type_1">专用发票</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="invoice_type" id="invoice_type_2" value="2" <?php if(2==$_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['invoice_type']){echo "checked='checked'";}?> />&nbsp;<label for="invoice_type_2">普通发票</label></td>
	</tr>
	<tr>
		<td align="right">合同备注：</td>
		<td align="left">&nbsp;<textarea name="tips" id="tips" cols="50" rows="4"><?=$_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['tips']?$_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['tips']:""?></textarea></td>
	</tr>
    <?php if("LEVEL-4" == $_SESSION['identity']['level']):?>
    <tr>
        <td align="right">签约日期：</td>
        <td align="left">&nbsp;<input name="sign_date" id="sign_date" value="<?=$_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['sign_date']?$_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['sign_date']:""?>"/><a href="javascript:void(0)" onclick="saveSignDate()">保存</a></td>
    </tr>
    <?php endif;?>
	<tr>
		<td align="right">经销商明细：</td>
		<td align="left"><?=$_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['dealers_str']?"<br>&nbsp;".$_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['dealers_str']."<br>":""?><input type="button" value="添加经销商" onclick="AddDealers()" /></td>
	</tr>
</table>
<br />
<input type="button" value="添加产品" onclick="AddSkuInToCart()" /><br>
<table border="1" width="100%">
	<tr>
		<th>产品</th>
		<th>刊例单价</th>
		<th>数量</th>
		<th>刊例价</th>
		<th>标准售价</th>
		<th>实际售价</th>
		<th>返点</th>
		<th>操作</th>
	</tr>
	<?php $is_chezhitui = 0;?>
	<?php if(!empty($_SESSION['contract-edit-type2'][$this->contract_id]['sku_info'])):?>
	<?php foreach($_SESSION['contract-edit-type2'][$this->contract_id]['sku_info'] as $id => $val):?>
	<?php if(2==$val['product_id']){$is_chezhitui=1;}//判断有没有车智推产品存在?>
	<tr>
		<td><?=$val['sku_path']?>
            <?php if($val['product_id'] == '2'):?>
                <br/><span style="color:red">汇销宝账号：<?=$val['hxb_account']?></span>
            <?php endif;?>
        </td>
		<td><?=$val['unit_price']?></td>
		<td><?=$val['sku_quantity']?></td>
		<td><?=$val['unit_price']*$val['sku_quantity']?></td>
		
		<?php if(961 == $val['sku_id']):?>
		<td><?=$val['unit_price']*$val['sku_quantity']?></td>
		<?php else:?>
		<td><?=$val['unit_price']*$val['sku_quantity']/2?></td>
		<?php endif;?>
		
		<td><?=$val['actual_pay']?></td>
		<td>
		<?php if(961 == $val['sku_id']):?>
		<?=($val['961_bonus'] / $val['actual_pay'] * 100)?>%
		<?php endif;?>
		</td>
		<td><input type="button" value="删除" onclick="DelSku(<?=$id?>, <?=$this->contract_id?>)" /></td>
	</tr>
	<?php endforeach;?>
	<?php endif;?>
</table>

<?php if(isset($_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['contract_value'])):?>
<?php if(0 < $_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['contract_value']):?>
<br />
<a name="pay-plan"></a>
<input type="button" value="添加回款计划" onclick="AddPayPlan()" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: orange; font-size: 9px">(回款总额需等于合同实际金额：<?=$_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['contract_value']?>元)</span>
<br>
<table border="1" width="100%">
	<tr>
		<th>计划回款日期</th>
		<th>金额</th>
		<th>备注</th>
		<th>发票号</th>
		<th>状态</th>
		<th>操作</th>
	</tr>
	<?php if(!empty($_SESSION['contract-edit-type2'][$this->contract_id]['pay_plan'])):?>
	<?php foreach($_SESSION['contract-edit-type2'][$this->contract_id]['pay_plan'] as $id => $val):?>
	<tr>
		<td><?=$val['plan_date']?></td>
		<td><?=$val['plan_value']?></td>
		<td><?=$val['tips']?></td>
		<td><?=$val['invoice']?></td>
		<td><?=$val['status']?"<font color='green'>已认款</font>":"未认款"?></td>
		<td><?php if(!$val['status']):?><input type="button" value="删除" onclick="DelPayPlan(<?=$id?>, <?=$this->contract_id?>)" /><?php endif;?></td>
	</tr>
	<?php endforeach;?>
	<?php endif;?>
</table>
<?php endif;?>
<?php endif;?>
<br /><br />
<div align="center"><input type="button" class="buttonlong" value="提交审核" onclick="submitform();return false;" /></div>
<br /><br />
<br /><br />
<input type="hidden" name="contract_id" value="<?=$this->contract_id?>" />
</form>
</div>
<br/><br/>
</div>

<script>
<?php if(2!=$_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['sign_type']):?>
$("#key_account_sales_input").hide();
<?php endif;?>
var contract_id = <?=$this->contract_id?>;

$(function() {
    $( "#sign_date" ).datepicker({ dateFormat: 'yy-mm-dd' });
});

function saveSignDate(){
    sign_date = $("#sign_date").val()
    $.ajax({
        type: "post",
        url : "/ajax/save-contract-sign-date",
        dataType:'json',
        async:false,
        data: {
            'contract_id': contract_id,
            'sign_date': sign_date
        },
        success: function(){
            alert("修改成功");
            history.back()
        }
    });
}

function DisplaySales(sign_type)
{
    if(sign_type == 1)
    {
        $("#key_account_sales_input").hide();
    }else{
        $("#key_account_sales_input").show();
    }
}


function num(th){
	th.value = th.value.replace(/[^\d\.]*$/g, '');
	
	var contract_value = parseFloat(<?=$_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['contract_value']?>);
	
	document.getElementById("receivable_value").innerHTML = contract_value;
}

function DelSku(id, contract_id)
{
	var msg = "确定要删除吗？"; 
	if (confirm(msg)==true){ 
		window.location.href='/contracts/del-sku-from-contract/id/' + id + '/contract_id/' + contract_id;
	}else{ 
		return false; 
	} 
}

function DelPayPlan(id, contract_id)
{
	var msg = "确定要删除吗？"; 
	if (confirm(msg)==true){ 
		window.location.href='/contracts/del-pay-plan-from-contract/id/' + id + '/contract_id/' + contract_id;
	}else{ 
		return false; 
	} 
}

function submitform() 
{
	var is_chezihtui = <?=$is_chezhitui?>;
	
	if( document.form1.owner_id.value.length == 0 )
	{
		alert("乙方单位不能为空。");
		return false;
	}else if( document.form1.signer_id.value.length == 0 )
	{
		alert("签约公司不能为空。");
		return false;
	}else if( document.form1.sign_type.value == 2 && document.form1.key_account_sales.value.length == 0 )
	{
		alert("集采需选择大客户销售。");
		return false;
	}else if( is_chezihtui == 1 && (document.form1.pay_type.value == 2 || document.form1.pay_type.value == 3) )
	{
		alert("车智推合同不允许后付或分期的付款方式。");
		return false;
	}
	
	<?php if(empty($_SESSION['contract-edit-type2'][$this->contract_id]['sku_info'])):?>
		alert("您未添加产品，不可提交合同。");
		return false;
	<?php endif;?>
	
	<?php
	$contract_days = 0;
	
	if($_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['start_date'] && $_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['expired_date'])
	{
		$start_date = $_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['start_date'];
		$expired_date = $_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['expired_date'];
		$start_date_timestamp = mktime(0,0,0,substr($start_date,5,2),substr($start_date,8,2),substr($start_date,0,4));
		$expired_date_timestamp = mktime(0,0,0,substr($expired_date,5,2),substr($expired_date,8,2),substr($expired_date,0,4));
		
		$contract_days = round(( $expired_date_timestamp - $start_date_timestamp ) / (3600 * 24)) + 1;
	}
	?>
	
	<?php if(1 == $_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['contract_type_id'] && 31 > $contract_days):?>
		if( document.form1.tips.value.length == 0 )
		{
			alert("付费合同的服务期限低于一个月时，需在备注处填写原因");
			return false;
		}
	<?php endif;?>
	
	<?php if(2 == $_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['contract_type_id'] && 31 <= $contract_days):?>
		if( document.form1.tips.value.length == 0 )
		{
			alert("免费合同的服务期限超过一个月时，需在备注处填写原因");
			return false;
		}
	<?php endif;?>

	<?php if(isset($_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['contract_value'])):?>
	<?php if(0 < $_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['contract_value']):?>
	
	<?php
	$plan_total = 0;
	$plan_total2 = 0;
	
	if($_SESSION['contract-edit-type2'][$this->contract_id]['pay_plan'])
	{
		foreach($_SESSION['contract-edit-type2'][$this->contract_id]['pay_plan'] as $id => $val)
		{
			$plan_total += $val['plan_value'];
			
			if($val['plan_value'] > 0)
			{
				$plan_total2 += $val['plan_value'];
			}
		}
	}
	?>
			
	<?php if(round($plan_total,2) != round($_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['contract_value'],2) && round($plan_total2,2) != round($_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['contract_value'],2) && "LEVEL-3" != $_SESSION['identity']['level']):?>
	alert("回款总额需等于合同实际金额：<?=$_SESSION['contract-edit-type2'][$this->contract_id]['contract_info']['contract_value']?>元，请修改回款计划。");
	return false;
	<?php endif;?>
	
	<?php endif;?>
	<?php endif;?>
	
	form1.submit();
}

function SavePageInfo()
{
	var signer_id = document.getElementById("signer_id").value;
	var owner_id = document.getElementById("owner_id").value;
	var pay_type = document.getElementById("pay_type").value;
	var tips = document.getElementById("tips").value;
	var contract_id = <?=$this->contract_id?>;
	var sign_type = document.getElementById("sign_type").value;
	var invoice_type = $("input[name='invoice_type']:checked").val();
	var key_account_sales = '';
	
	if(sign_type == 2)
	{
		key_account_sales = document.getElementById("key_account_sales").value;
	}
	
	$.ajax({
			type: "post",
			url : "/ajax/save-contract-info-to-session",
			dataType:'json',
            async:false,
			data: {
					'signer_id': signer_id,
					'owner_id': owner_id,
					'pay_type': pay_type,
					'tips': tips,
					'contract_id': contract_id,
					'sign_type': sign_type,
					'invoice_type': invoice_type,
					'key_account_sales': key_account_sales
				  },
				success: function(){
					
				}
		});
}

function AddSkuInToCart()
{
	SavePageInfo();
	window.location.href = '/contracts/add-sku-into-contract/contract_id/<?=$this->contract_id?>';
}

function AddPayPlan()
{
	SavePageInfo();
	window.location.href = '/contracts/add-pay-plan/contract_id/<?=$this->contract_id?>';
}

function AddDealers()
{
	SavePageInfo();
	window.location.href = '/contracts/add-dealers-into-contract/contract_id/<?=$this->contract_id?>';
}
</script>
